package cn.machen.www.fashion.controller;

import cn.machen.www.fashion.services.ProductService;
import cn.machen.www.fashion.utils.BaseServlet;
import org.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ProductServlet extends BaseServlet {
    private ProductService productService = new ProductService();


    /*查询所有商品*/
    public String findAll(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("findall");
        request.setAttribute("products",productService.findAll());
        request.getRequestDispatcher("/jsps/fashion/list.jsp").forward(request,response);
        return null;
    }

    /*根据商品分类查询所有商品*/
    public String findByCid(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("cid");
        String cid = request.getParameter("cid");
        request.setAttribute("products",productService.findByCid(cid));
        request.getRequestDispatcher("/catalog.jsp").forward(request,response);
        return null;
    }

    /*加载商品详情*/
    public String load(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("load");

        String pid = request.getParameter("pid");
        System.out.println(pid);
        request.setAttribute("product",productService.findSingleProduct(pid));
        request.getRequestDispatcher("/prodes.jsp").forward(request,response);
        return null;
    }



}
